package IO;

import org.junit.Test;
import java.io.*;

public class ObjectInputOutputStream {
    @Test
    public void test1(){
        //创建数据写入流
        try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("./user.txt"))) {
            //将内容写入文档
            oos.writeObject(new String("喵喵喵"));
            oos.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    @Test
    public void test2(){
        //创建数据读取流
        try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream("./user.txt"))) {
            //读取文档,需要按写入的顺序读取
            Object object = ois.readObject();
            String s=(String)object;

        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
}
